/*
 * @Author: 哈尔滨大拐 3181845089@qq.com
 * @Date: 2022-07-23 11:12:53
 * @LastEditors: 哈尔滨大拐 3181845089@qq.com
 * @LastEditTime: 2022-08-09 11:17:08
 * @FilePath: \mygit\个人网站\js\main.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*--------------------------------------------------------------
  2. 页面初始化，计算rem
--------------------------------------------------------------*/
// 函数立即调用
(function () {
    const buildRem = function () {
        let designSize = 1920;//设计图尺寸
        let html = document.documentElement;
        let wW = html.clientWidth;//窗口宽度
        let rem = wW * 100 / designSize;
        html.style.fontSize = rem + 'px'
    }
    // 初始化
    buildRem()
    //页面缩放时 font-size 的调整
    window.onresize = buildRem;
})();

// 导航栏加亮
// (function(){
//     let page = location.search.match(/^\?page=(\w+)/)[1];
//     document.querySelector('.' + page).classList.add('active')
// })();

// 动态插入header
(function () {
    const header = document.querySelector('#header')
    if (header) {
        const navData = [
            {
                title: "首页",
                url: "index.html",
            },
            {
                title: "作品展示",
                url: "protfolio.html",
            },
            {
                title: "博客",
                url: "blogs.html",
            },
            {
                title: "我的简历",
                url: "about.html",
            },
            {
                title: "联系我",
                url: "contact.html",
            },
        ]
        const insertNav = function () {
            let res = ''
            navData.forEach(nav => {
                res += `<li class="${getPageName() == nav.url ? 'active' : ''}"><a href="${nav.url}">${nav.title}</a></li>`
            })
            return res
        }

        header.insertAdjacentHTML('afterbegin', `
        <div class="container flex align-items-center justify-content-between">
                <a href="./index.html"><img class="logo" src="./images/logo.png" alt=""></a>
                <nav id="navbar" class="navbar">
                <ul>${insertNav()}</ul>
              </nav>
            </div>
        `)

        function getPageName() {
            const pathArr = location.pathname.split('/')
            return pathArr[pathArr.length - 1]
        }
    }
})();


 // 回到顶部
 (function () {
    let up = document.querySelector('#up')
    const html = document.documentElement

    document.addEventListener('scroll', function () {
        up.hidden = !(html.scrollTop > html.clientHeight)
    }, false);


    up.addEventListener('click', function () {
        html.scrollTo({
            top: 0,
            behavior: "smooth"
        })
    }, false)
})()


const base_url = 'http://81.70.162.221:3000'
axios.defaults.baseURL = base_url

function imgUrl(url) {
  return base_url + '/' + url
}
function GetQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]); return null;
}


// 获取当前时间
let times
function addZero(num) {
    return num < 10 ? '0' + num : num + ''
}
// 年
function getYear(date) {
    return date.getFullYear()
}
// 月
function getMonth(date) {
    return date.getMonth() + 1
}
// 日
function getDate(date) {
    return date.getDate()
}

function getFullDay(date) {
    times = `${getYear(date)}年${addZero(getMonth(date))}月${addZero(getDate(date))}日`

    return times
}
// console.log(getFullDay(new Date()));